% test linear discriminant analysis(LDA) using matlab

clear
close all

%generate data
v1=mvnrnd([1, 1], [0.3, 0.1; 0.1, 0.4], 300);
v2=mvnrnd([0, 0], [0.5, -0.2; -0.2, 0.1], 200);

data=[v1;v2];
classes=[ones(length(v1), 1);zeros(length(v2),1)];

gscatter(data(:,1), data(:,2), classes);

%create default LDA
linclass = ClassificationDiscriminant.fit(data, classes);

%visualize the classification boundary

hold on
K=linclass.Coeffs(1,2).Const;
L=linclass.Coeffs(1,2).Linear;

f=@(x1, x2) K+L(1)*x1+L(2)*x2;
ezplot(f, [-2 4 -1 4]);


%five-fold cross validation
cvmodel=crossval(linclass, 'kfold', 5);
cverror=kfoldLoss(cvmodel)

